'use strict';

const { postJSON } = require('./util');

/**
 * 创建自定义菜单
 * 详细请看：<http://qydev.weixin.qq.com/wiki/index.php?title=创建应用菜单>
 *
 * Menu:
 *    {
 *        "button":[
 *            {
 *                "type":"click",
 *                "name":"今日歌曲",
 *                "key":"V1001_TODAY_MUSIC"
 *            },  
 *            { 
 *                "name":"菜单",
 *                "sub_button":[
 *                  {
 *                    "type":"view",
 *                    "name":"搜索",
 *                    "url":"http://www.soso.com/"
 *                  },
 *                  {
 *                    "type":"click",
 *                    "name":"赞一下我们",
 *                    "key":"V1001_GOOD"
 *                  }]
 *                }]
 *            }
 *        ]
 *    }
 *
 * Examples:
 *    var result = await api.createMenu(menu);
 *
 * Result:
 *    {"errcode":0,"errmsg":"ok"}
 *
 * @param {Object} menu 菜单对象
 */
exports.createMenu = async function (menu) {
  const { accessToken } = await this.ensureAccessToken();
  var url = this.prefix + 'menu/create?agentid=' + this.agentid + '&access_token=' + accessToken;
  return this.request(url, postJSON(menu));
};

/**
 * 获取菜单
 * 详细请看：<http://qydev.weixin.qq.com/wiki/index.php?title=获取菜单列表>
 *
 * Examples:
 *    await api.getMenu();
 *
 * Result:
 *    {
 *        "menu": {
 *          "button":[
 *            {"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC","sub_button":[]},
 *            {"type":"click","name":"歌手简介","key":"V1001_TODAY_SINGER","sub_button":[]},
 *            {"name":"菜单","sub_button":[
 *              {"type":"view","name":"搜索","url":"http://www.soso.com/","sub_button":[]},
 *              {"type":"view","name":"视频","url":"http://v.qq.com/","sub_button":[]},
 *                 {"type":"click","name":"赞一下我们","key":"V1001_GOOD","sub_button":[]}]
 *            }
 *          ]
 *        }
 *    }
 *
 */
exports.getMenu = async function () {
  const { accessToken } = await this.ensureAccessToken();
  var url = this.prefix + 'menu/get?agentid=' + this.agentid + '&access_token=' + accessToken;
  return this.request(url);
};

/**
 * 删除自定义菜单
 * 详细请看：<http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口>
 *   
 * Examples:
 *    var result = await api.removeMenu();
 *
 * Result:
 *    {"errcode":0,"errmsg":"ok"}
 *
 */
exports.removeMenu = async function () {
  const { accessToken } = await this.ensureAccessToken();
  var url = this.prefix + 'menu/delete?agentid=' + this.agentid + '&access_token=' + accessToken;
  return this.request(url);
};
